#!/bin/bash

# Chat Server 测试脚本
# 阶段4：Chat Server 实战测试

echo "========================================"
echo "  Chat Server Test Script"
echo "  Stage 4: Chat Server Implementation"
echo "========================================"
echo ""

# 检查可执行文件是否存在
if [ ! -f "build/chat_server" ]; then
    echo "Error: chat_server not found. Please compile first:"
    echo "  cd build && cmake .. && make chat_server"
    exit 1
fi

if [ ! -f "build/chat_client" ]; then
    echo "Error: chat_client not found. Please compile first:"
    echo "  cd build && cmake .. && make chat_client"
    exit 1
fi

echo "Step 1: Starting Chat Server..."
echo "----------------------------------------"
./build/chat_server config/chat.json &
SERVER_PID=$!
echo "Chat Server PID: $SERVER_PID"
echo ""

# 等待服务器启动
sleep 2

echo "Step 2: Server is ready!"
echo "----------------------------------------"
echo "You can now connect using:"
echo "  Terminal 1: ./build/chat_client 127.0.0.1 Alice"
echo "  Terminal 2: ./build/chat_client 127.0.0.1 Bob"
echo "  Terminal 3: ./build/chat_client 127.0.0.1 Charlie"
echo ""
echo "Commands in chat client:"
echo "  /help          - Show help"
echo "  /users         - List online users"
echo "  /private <user> <msg> - Send private message"
echo "  /quit          - Quit"
echo "  <message>      - Send public message"
echo ""
echo "Press Ctrl+C to stop the server"
echo "========================================"
echo ""

# 等待用户中断
trap "echo ''; echo 'Stopping Chat Server...'; kill $SERVER_PID 2>/dev/null; wait $SERVER_PID 2>/dev/null; echo 'Server stopped.'; exit 0" INT TERM

# 保持脚本运行
wait $SERVER_PID

